package com.heima.article.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    //mybatisPlus对于多表查询并不好用，所以仍需注入mapper，
    @Autowired
    private ApArticleMapper apArticleMapper;

    private final static short MAX_PAGE_SIZE=50;
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {

        //参数校验
        //校验分页条数
        Integer size = dto.getSize();
        if (size == null|| size==0) {
            size=10;
        }
        size = Math.min(size,MAX_PAGE_SIZE);
        //校验type
        if (!type.equals(ArticleConstants.LOADTYPE_LOAD_MORE)&&!type.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){

            type=ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //校验频道参数
        if (StringUtils.isBlank(dto.getTag())) {

            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }

        //校验时间参数
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }
        //查询
        List<ApArticle> articleList = apArticleMapper.loadArticleList(dto, type);
        //结果返回
        return ResponseResult.okResult(articleList);
    }

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {

//        log.info("//服务降级测试");
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        //1.检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = new ApArticle();
        log.info("从dto获取文章字段");
        BeanUtils.copyProperties(dto,apArticle);
//        System.out.println("########@@@@@@@@%%%%%%%%%");
//        System.out.println("dto.getId():"+dto.getId());
//        System.out.println("########@@@@@@@@%%%%%%%%%");
        //2.判断是否存在ID
        if (dto.getId() == null) {
            //2.1 不存在id，保存文章、文章配置、文章内容
            //保存文章
            log.info("保存文章ap_article表");
            save(apArticle);
            //保存配置
            //自动生成雪花ID，构造器-给其他都赋予默认值，只需获取文章ID，
            ApArticleConfig apArticleConfig=new ApArticleConfig(apArticle.getId());
//            apArticleConfig.setArticleId(apArticle.getId());
            log.info("保存文章配置ap_article_config表");
            apArticleConfigMapper.insert(apArticleConfig);
            //保存文章内容
            ApArticleContent apArticleContent=new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            log.info("从dto获取文章内容");
            apArticleContent.setContent(dto.getContent());
            log.info("保存文章内容ap_article_content表");
            apArticleContentMapper.insert(apArticleContent);
        }else {
            //2.2 存在id，修改文章、文章内容
            //修改文章
            log.info("更新文章ap_article表");
            updateById(apArticle);
            //修改文章内容
            log.info("查询数据库，获取文章内容实体类");
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(
                    Wrappers.<ApArticleContent>lambdaQuery().eq(
                            ApArticleContent::getArticleId,dto.getId()
                    )
            );
//            System.out.println("########@@@@@@@@%%%%%%%%%");
//            System.out.println("apArticleContent:"+apArticleContent);
//            System.out.println("########@@@@@@@@%%%%%%%%%");
//            System.out.println("########@@@@@@@@%%%%%%%%%");
//            System.out.println("dto.getContent()"+dto.getContent());
//            System.out.println("########@@@@@@@@%%%%%%%%%");
            log.info("从dto获取文章内容字段，设为数据库字段");
            apArticleContent.setContent(dto.getContent());
            log.info("更新文章内容ap_article_content表");
            apArticleContentMapper.updateById(apArticleContent);
            log.info("修改文章功能不能修改文章配置，下架，删除，评论转发，属于其他功能");
        }

        //3. 结果返回，文章的id
        return ResponseResult.okResult(apArticle.getId());
    }
}
